<template>
  <!--弹出窗-->
  <modal ref="modal_1"
         :title="title"
         :isCloseConfirm="this.editable"
         :fullscreen="this.fullscreen"
         top="5%"
         width="50%"
         :close-on-click-modal="true"
         @modalOpened="opened"
         @modalClose="closed">

    <avue-form :option="option"
               ref="form"
               v-model="form"
               @submit="onSubmit"
               :upload-before="uploadBefore"
               :upload-after="uploadAfter"

               :upload-exceed="uploadExceed"
               :upload-error="uploadError">

    </avue-form>

    <!--底部按钮块, 默认只有"取消"按钮-->
    <template #footerButton>
      <el-button @click="$refs.modal_1.handleClose()">关闭</el-button>
    </template>

  </modal>
</template>

<script>
    import {getList, getDetail, add, update, remove, getMaterialFront} from "@/api/anwise/material/material";
    import {checkBomChild} from "@/api/anwise/material/bom_list";
    import {selectCustomerGroup, generatePackLabel} from "@/api/anwise/material/customer_group";
  import {mapGetters} from "vuex";
    import {Pattern} from "@/utils/validate.js";


  export default {

    data() {
      return {
        emits: ["success"],
          selectedNodeNo:'',
          customerGroupList:[],
          selectModelList:[],
        editable: false,
        title: "物料图片",
        isCloseConfirm: false,
        fullscreen: false,
        form: {},
        query: {},
        loading: true,
          btnLoading: false,
        option: {
          height: 'auto',
          calcHeight: 30,
          labelWidth: 150,
          menuWidth: 120,
          submitBtn: false,
          emptyBtn: false,
          column: [
              {
                  label: "",
                  prop: "place",
                  type: "",
                  detail:true,
                  span: 24,
                  placeholder: ' '

                  // labelPosition: "top",
              },
              {
                  label: "物料图片",
                  prop: "fileList",
                  type: "upload",
                  listType: 'picture-card',
                  action: '/blade-resource/file/upload',
                  accept:'.png,.jpg,.jpeg,.bpm,.gif',
                  // fileType: 'img',
                  // tip: '只能上传png/jpg/jpeg/bpm/gif，且不超过10M，数量不超过10张',
                  limit: 10,
                  fileSize: 10000,
                  span: 24,
                  detail:true,
                  propsHttp: {
                      res: 'data',
                      url: 'url',
                  },
                  // canvasOption: {
                  //     text: 'ANWISE ERP CLOUD',
                  //     ratio: 0.1,
                  // },
              },
          ]
          },
          data: []
        };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.validData(this.permission.material_add, false),
          viewBtn: this.validData(this.permission.material_view, false),
          delBtn: this.validData(this.permission.material_delete, false),
          editBtn: this.validData(this.permission.material_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
      mounted() {
          // this.initDict();
      },
    methods: {
        show(title,id) {
            setTimeout(() => {
                getDetail(id).then(res =>{
                    if (res.data.success) {
                        this.form = res.data.data;
                        this.form.imgList.forEach(item =>{
                            if (!this.form.fileList) {
                                this.form.fileList = [];
                            }
                            this.form.fileList.push(item.url);
                        })
                    }
                })
            }, 500);
            this.title=title;
            this.$refs.modal_1.show();
        },
        onSubmit(form, done) {
        },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      },
        opened() {
            console.log("opened")
        },
        closed() {
            this.form={}
        },
        uploadBefore (file, done, loading, column) {
            console.log(file, column)
            // var newFile = new File([file], '1234', { type: file.type });
            done(file)
            // this.$message.success('上传前的方法')
        },
        uploadAfter (res, done, loading, column) {
            console.log(res, column)
            if (!this.form.imgList) {
                this.form.imgList = [];
            }
            this.form.imgList.push(res);
            done()
            this.$message.success('上传成功！')
        },
        uploadDelete (file, column) {
            console.log(file, column)
            return new Promise((resolve, reject) => {
                if (this.form.imgList) {
                    this.form.imgList.forEach(item => {
                        if (item.url === file.url) {
                            console.log("删除项", item.url, file.url);
                            console.log(this.form.imgList.splice(item,1));
                        }
                        console.log(this.form.imgList);
                    })
                }
                resolve();
            });

        },
        uploadExceed (limit, files, fileList, column) {
            console.log(limit, files, fileList, column)
            this.$message.error('超出上传限制文件数量')
        },
        uploadError (error, column) {
            this.$message.error('上传失败！')
            console.log(error, column)
        }
    }
  };
</script>

<style>
</style>
